package com.liaowei.comment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liaowei.comment.model.param.ArticleCommentParam;
import com.liaowei.comment.service.ArticleCommentService;
import com.liaowei.common.BaseResponse;
import com.liaowei.common.ResultUtils;
import com.liaowei.model.domain.ArticleComment;
import com.liaowei.model.param.QueryManageArticleCommentParam;
import com.liaowei.model.vo.ArticleCommentVO;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("articles")
@RequiredArgsConstructor
@Validated
public class ArticleCommentController {

    private final ArticleCommentService articleCommentService;


    // 查询用户是否对该文章评论过
    @GetMapping("isComment/{articleId}/{userId}")
    public BaseResponse<List<ArticleComment>> isCommented(@PathVariable("articleId") @NotNull(message = "文章id不能为空") Long articleId,
                                                          @PathVariable("userId") @NotNull(message = "评论id不能为空") Long userId){
        System.out.println(articleId);
        System.out.println(userId);
        return ResultUtils.success(articleCommentService.searchIsCommented(articleId, userId));
    }

    // 后台-分页查询评论列表
    @GetMapping("manage/page")
    public BaseResponse<IPage<ArticleCommentVO>> managePage(int pageNum, int pageSize) {
        return ResultUtils.success(articleCommentService.searchManagePage(pageNum, pageSize));
    }


    // 分页查询评论列表
    @PostMapping("page")
    public BaseResponse<IPage<ArticleCommentVO>> page(@RequestBody
                                                      @Validated
                                                      QueryManageArticleCommentParam commentParam) {
        return ResultUtils.success(articleCommentService.searchPage(commentParam));
    }

    // 根据文章id删除文章的评论
    @DeleteMapping("/{articleId}/{userId}")
    public BaseResponse<String> deleteComment(@PathVariable("articleId") @NotNull(message = "文章id不能为空") Long articleId,
                                              @PathVariable("userId") @NotNull(message = "评论id不能为空") Long userId) {
        return ResultUtils.success(articleCommentService.deleteArticleCommentByIdAndUserId(articleId, userId));
    }

    // 删除评论
    @DeleteMapping("{commentId}/{articleId}/{userId}")
    public BaseResponse<String> deleteComment(@PathVariable("commentId") @NotNull(message = "评论id不能为空") Long commentId,
                                              @PathVariable("articleId") @NotNull(message = "文章id不能为空") Long articleId,
                                              @PathVariable("userId") @NotNull(message = "评论id不能为空") Long userId) {
        return ResultUtils.success(articleCommentService.deleteArticleComment(commentId, articleId, userId));
    }

    // 评论文章
    @PostMapping
    public BaseResponse<String> commentArticle(@RequestBody @Validated ArticleCommentParam commentParam) {
        return ResultUtils.success(articleCommentService.saveArticleComment(commentParam));
    }
}
